Param ( [Parameter(Mandatory = $false)] [string]$ExcludeUsers = "", [Parameter(Mandatory = $false)] [string]$BrowsingHistory = "false", [Parameter(Mandatory = $false)] [string]$Cache = "false", [Parameter(Mandatory = $false)] [string]$Cookies = "false", [Parameter(Mandatory = $false)] [string]$FullUserDataRemoval = "false", [Parameter(Mandatory = $false)] [string]$ConfirmFullRemoval = "false" ) $ExcludedUserList = @() if (-not [string]::IsNullOrWhiteSpace($ExcludeUsers)) { $ExcludedUserList = $ExcludeUsers.Split(',') | ForEach-Object { $_.Trim().ToLower() } } $EdgeProcesses = Get-Process -Name "msedge" -ErrorAction SilentlyContinue if ($EdgeProcesses) { Write-Host "Closing all running Microsoft Edge instances..." Stop-Process -Name "msedge" -Force Start-Sleep -Seconds 3 } [long]$TotalBytesFreed = 0 $ClearedSettingsList = [System.Collections.Generic.List[string]]::new() function Remove-BrowserData { param( [string[]]$Paths ) [long]$BytesFreed = 0 foreach ($Path in $Paths) { if (-not (Test-Path $Path)) { continue } try { $Items = Get-Item -LiteralPath $Path -Force foreach ($Item in $Items) { if ($Item.Attributes -band [System.IO.FileAttributes]::ReparsePoint) { continue } if ($Item.PSIsContainer) { $Size = ( Get-ChildItem -LiteralPath $Item.FullName -File -Recurse -Force -ErrorAction SilentlyContinue | Measure-Object Length -Sum ).Sum if ($Size) { $BytesFreed += $Size } Get-ChildItem -LiteralPath $Item.FullName -Force -ErrorAction SilentlyContinue | Where-Object { -not ($_.Attributes -band [System.IO.FileAttributes]::ReparsePoint) } | Remove-Item -Recurse -Force -ErrorAction SilentlyContinue } else { $BytesFreed += $Item.Length Remove-Item -LiteralPath $Item.FullName -Force -ErrorAction SilentlyContinue } } } catch {} } return $BytesFreed } $CurrentIdentity = [Security.Principal.WindowsIdentity]::GetCurrent() $IsAdmin = (New-Object Security.Principal.WindowsPrincipal($CurrentIdentity)).IsInRole( [Security.Principal.WindowsBuiltInRole]::Administrator ) $TargetPaths = @() if ($IsAdmin) { Get-ChildItem "C:\Users" -Directory | ForEach-Object { $UserName = $_.Name if ($UserName -in @("All Users","Default","Default User","Public","desktop.ini")) { return } if ($ExcludedUserList -contains $UserName.ToLower()) { return } $EdgePath = "$($_.FullName)\AppData\Local\Microsoft\Edge\User Data" if (Test-Path $EdgePath) { $TargetPaths += [PSCustomObject]@{ UserName = $UserName EdgeBaseDir = $EdgePath } } } } else { $UserName = $env:USERNAME if ($ExcludedUserList -contains $UserName.ToLower()) { return } $EdgePath = "$env:LOCALAPPDATA\Microsoft\Edge\User Data" if (Test-Path $EdgePath) { $TargetPaths += [PSCustomObject]@{ UserName = $UserName EdgeBaseDir = $EdgePath } } } foreach ($Target in $TargetPaths) { $TargetUser = $Target.UserName $EdgeBaseDir = $Target.EdgeBaseDir [long]$UserBytesFreed = 0 $UserClearedSettings = @() if ($FullUserDataRemoval -eq "true") { if ($ConfirmFullRemoval -ne "true") { throw "FullUserDataRemoval requires ConfirmFullRemoval=true" } Write-Host "Processing user: $TargetUser" $Bytes = Remove-BrowserData -Paths @("$EdgeBaseDir\*") if ($Bytes -gt 0) { $UserBytesFreed += $Bytes $UserClearedSettings += "FullUserDataRemoval" if ("FullUserDataRemoval" -notin $ClearedSettingsList) { $ClearedSettingsList.Add("FullUserDataRemoval") } } } else { $EdgeProfiles = Get-ChildItem -Path $EdgeBaseDir -Directory | Where-Object { $_.Name -eq "Default" -or $_.Name -like "Profile*" } foreach ($Profile in $EdgeProfiles) { $ProfilePath = $Profile.FullName if ($Cache -eq "true") { $Bytes = Remove-BrowserData -Paths @( "$ProfilePath\Cache", "$ProfilePath\Code Cache", "$ProfilePath\GPUCache", "$ProfilePath\ShaderCache" ) if ($Bytes -gt 0) { $UserBytesFreed += $Bytes if ("Cache" -notin $UserClearedSettings) { $UserClearedSettings += "Cache" } if ("Cache" -notin $ClearedSettingsList) { $ClearedSettingsList.Add("Cache") } } } if ($Cookies -eq "true") { $Bytes = Remove-BrowserData -Paths @( "$ProfilePath\Network\Cookies", "$ProfilePath\Network\Cookies-journal" ) if ($Bytes -gt 0) { $UserBytesFreed += $Bytes if ("Cookies" -notin $UserClearedSettings) { $UserClearedSettings += "Cookies" } if ("Cookies" -notin $ClearedSettingsList) { $ClearedSettingsList.Add("Cookies") } } } if ($BrowsingHistory -eq "true") { $Bytes = Remove-BrowserData -Paths @( "$ProfilePath\History", "$ProfilePath\History-journal", "$ProfilePath\Top Sites", "$ProfilePath\Top Sites-journal", "$ProfilePath\Visited Links", "$ProfilePath\Web Data", "$ProfilePath\Web Data-journal", "$ProfilePath\Sessions", "$ProfilePath\Session Storage" ) if ($Bytes -gt 0) { $UserBytesFreed += $Bytes if ("BrowsingHistory" -notin $UserClearedSettings) { $UserClearedSettings += "BrowsingHistory" } if ("BrowsingHistory" -notin $ClearedSettingsList) { $ClearedSettingsList.Add("BrowsingHistory") } } } } } $TotalBytesFreed += $UserBytesFreed Write-Host "" Write-Host "Processing user: $TargetUser" if ($UserClearedSettings.Count -gt 0) { Write-Host "Cleared Settings: $($UserClearedSettings -join ', ')" } else { Write-Host "Cleared Settings: None" } Write-Host ("Space Freed : {0} MB" -f [Math]::Round($UserBytesFreed / 1MB, 2)) Write-Host "" } $SpaceFreedMB = [Math]::Round($TotalBytesFreed / 1MB, 2) Write-Host "----------------------------------------" Write-Host "Execution Result Summary:" Write-Host "Total Space Freed: $SpaceFreedMB MB" if ($ClearedSettingsList.Count -gt 0) { Write-Host "Edge Cleared Settings: $($ClearedSettingsList -join ', ')" } else { Write-Host "No settings modified." }